//
//  TimeView.m
//  XDBaseFrameWork
//
//  Created by XD-XY on 9/23/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "TimeView.h"

@implementation TimeView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self makeViews];
        self.hidden = YES;
    }
    return self;
}

-(void)makeViews
{
    self.dayNumber = 0;
    self.hourNumber = 0;
    self.secondNumber = 0;
    
    persentTime = [[NSString alloc] init];
    
    self.bgView = [[UIView alloc] initWithFrame:self.bounds];
    _bgView.backgroundColor = [UIColor blackColor];
    _bgView.alpha = 0.3;
    
    self.pickeBgView  = [UIView initViewRect:CGRectMake(0, self.frame.size.height-492/2.0f, UI_SCREEN_WIDTH, 492/2.0f) andBackGroudColor:[UIColor whiteColor]];
    
    self.titleLB = [UILabel initLabelText:@"请选择预约时间" andRect:CGRectMake(0,0,UI_SCREEN_WIDTH,33) andBackGroudColor:mClearColor andTextFont:[UIFont systemFontOfSize:17] andTextColor:UIColorFromRGB(0xff0e03) andTextAlignment:NSTextAlignmentCenter];
    
    self.pickerView  = [[UIPickerView alloc] initWithFrame:CGRectMake(0,33, 320,190)];
    _pickerView.showsSelectionIndicator = YES;
    _pickerView.delegate = self;
    _pickerView.dataSource =self;
    _pickerView.backgroundColor = [UIColor whiteColor];
    
    self.sureBtn = [UIButton initButton:CGRectMake((320-115)/2.0f, self.pickeBgView.frame.size.height-40, 115, 30) btnNorImage:LOADIMAGE(@"timesure@2x") btnDisableBtn:LOADIMAGE(@"timesure_dis@2x") btnTitle:@"确定" titleNorColor:UIColorFromRGB(0xffffff) titleSelectColor:UIColorFromRGB(0xffffff) titleFont:[UIFont systemFontOfSize:15]];
    [_sureBtn setTitle:@"已占用" forState:UIControlStateDisabled];
    [_sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewClick)];
    [_bgView addGestureRecognizer:tap];
    
    [self addSubview:_bgView];
    [self addSubview:_pickeBgView];
    
    [self.pickeBgView addSubview:_titleLB];
    [self.pickeBgView addSubview:_pickerView];
    [self.pickeBgView addSubview:_sureBtn];
    
    self.hourArray = [[NSArray alloc] initWithObjects:@"09点",@"10点",@"11点",@"12点",@"13点",@"14点",@"15点",@"16点",@"17点",@"18点",@"19点", nil];
    self.secondArray = [[NSArray alloc] initWithObjects:@"00分",@"30分", nil];
    
    self.dayArray = [[NSMutableArray alloc] init];
    [self getDayDatas];
}

#pragma mark
#pragma mark 到家和影棚拍摄 时间 1表示到家 2表示影棚 3表示医院
-(void)setSouceTime:(int)type
{
    timeType = type;
    if (type ==1){
//        self.hourArray = [[NSArray alloc] initWithObjects:@"01点",@"02点",@"03点",@"04点",@"05点",@"06点",@"07点",@"08点",@"09点",@"10点",@"11点",@"12点",@"13点",@"14点",@"15点",@"16点",@"17点",@"18点",@"19点",@"20点",@"21点",@"22点",@"23点",@"24点", nil];
//        self.secondArray = [[NSArray alloc] initWithObjects:@"00分",@"30分", nil];
        
        self.hourArray = [[NSArray alloc] initWithObjects:@"上午",@"下午", nil];
        
    }else if (type==2){
        self.hourArray = [[NSArray alloc] initWithObjects:@"上午",@"下午", nil];

//        self.hourArray = [[NSArray alloc] initWithObjects:@"10点",@"11点",@"12点",@"13点",@"14点",@"15点",@"16点",@"17点",@"18点",@"19点", nil];
//        self.secondArray = [[NSArray alloc] initWithObjects:@"00分",@"30分", nil];
    }else if(type == 3){

//        self.hourArray = [[NSArray alloc] initWithObjects:@"上午",@"下午", nil];

        self.hourArray = [[NSArray alloc] initWithObjects:@"00点",@"01点",@"02点",@"03点",@"04点",@"05点",@"06点",@"07点",@"08点",@"09点",@"10点",@"11点",@"12点",@"13点",@"14点",@"15点",@"16点",@"17点",@"18点",@"19点",@"20点",@"21点",@"22点",@"23点",@"23点", nil];
        self.secondArray = [[NSArray alloc] initWithObjects:@"00分",@"30分", nil];
        
        //医院
        if (timeType == 3){
            NSDate *now = [NSDate date];
            
            NSDateFormatter* dateFormat0 = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
            [dateFormat0 setDateFormat:@"yyyy-MM-dd hh:mm"];
            persentTime = [NSString stringWithFormat:@"%@",[dateFormat0 stringFromDate:now]];
            
            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSDateComponents *comps;
            comps = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[[NSDate alloc] init]];
            
            [_dayArray removeAllObjects];
            for (int i = 0;i<=30;i++){
                [comps setDay:+i];
                [comps setHour:0];
                [comps setMinute:0];
                [comps setSecond:0];
                NSDate * nowDate = [calendar dateByAddingComponents:comps toDate:now options:0];
                
                NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
                [dateFormat setDateFormat:@"yyyy-MM-dd"];
                NSString *  locationString = [dateFormat stringFromDate:nowDate];
                [_dayArray addObject:locationString];
            }
        }
        
    }else if (type == 4){
        
        self.hourArray = [[NSArray alloc] initWithObjects:@"上午",@"下午", nil];

//        self.hourArray = [[NSArray alloc] initWithObjects:@"2012年",@"2013年",@"2014年",@"2015年",@"2016年", nil];
//        self.secondArray = [[NSArray alloc] initWithObjects:@"00分",@"30分", nil];
    }
    
    [self.pickerView selectRow:0 inComponent:0 animated:NO];
    [self.pickerView selectRow:0 inComponent:1 animated:NO];
//    [self.pickerView selectRow:0 inComponent:2 animated:NO];
    self.dayNumber = 0;
    self.hourNumber = 0;
    self.secondNumber = 0;
    [self.pickerView reloadAllComponents];
    
    if (timeType == 3){
        NSString * dayName = [self.dayArray objectAtIndex:self.dayNumber];
        NSString * hourName = [self.hourArray objectAtIndex:self.hourNumber];
       
        NSString * secondName = [self.secondArray objectAtIndex:self.secondNumber];
        NSString * tmpt = [NSString stringWithFormat:@"%@ %@:%@",dayName,[[hourName componentsSeparatedByString:@"点"] firstObject],[[secondName componentsSeparatedByString:@"分"] firstObject]];
        if ([persentTime compare:tmpt]==1){
            _sureBtn.enabled = NO;
        }else{
            _sureBtn.enabled = YES;
        }
        return;
        
    }

}

#pragma mark
#pragma mark UIPickeView Delegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (timeType == 3){
        return 3;
    }
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return [_dayArray count];
            break;
        case 1:
            return [_hourArray count];
            break;
        case 2:
            return [_secondArray count];
            break;
        
        default:
            break;
    }
    return 1;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component) {
        case 0:
        {
            return [_dayArray objectAtIndex:row];
        }
            break;
        case 1:
        {
            return [_hourArray objectAtIndex:row];
        }
            break;
        case 2:
        {
            return [_secondArray objectAtIndex:row];
        }
            break;
        default:
            break;
    }
    return nil;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            self.dayNumber = row;
            break;
        case 1:
            self.hourNumber = row;
            break;
        case 2:
            self.secondNumber = row;
            break;
        default:
            break;
    }
    
    NSString * dayName = [self.dayArray objectAtIndex:self.dayNumber];
    NSString * hourName = [self.hourArray objectAtIndex:self.hourNumber];
    if (timeType == 3){
        NSString * secondName = [self.secondArray objectAtIndex:self.secondNumber];
        NSString * tmpt = [NSString stringWithFormat:@"%@ %@:%@",dayName,[[hourName componentsSeparatedByString:@"点"] firstObject],[[secondName componentsSeparatedByString:@"分"] firstObject]];
        if ([persentTime compare:tmpt]==1){
            _sureBtn.enabled = NO;
        }else{
            _sureBtn.enabled = YES;
        }
        return;
    }
    
    
    BOOL isenable = YES;
    for (NSDictionary * dict in _yBuseDateArray){
        if ([dayName isEqualToString:dict[@"ShootDate"]]){
            _sureBtn.enabled = NO;
            isenable = NO;
            break;
        }
    }
    for (NSDictionary * dict in _userDateArray){
        if ([dayName isEqualToString:dict[@"ShootDate"]]&&[hourName isEqualToString:dict[@"ItemName"]]){
            _sureBtn.enabled = NO;
            isenable = NO;
            break;
        }
    }
    
    if (isenable){
        _sureBtn.enabled = YES;
    }
    
    if (_dayNumber == 0){
        if (_hourNumber == 0){
            _sureBtn.enabled = NO;
        }else{
            if ([persentTime intValue]>12){
                _sureBtn.enabled = NO;
            }
        }
    }
    
//    BOOL isenable = YES;
//    for (NSDictionary * dict in self.userDateArray){
    
//        if ([[dict valueForKey:@"ShootDate"] isEqualToString:dayName]){
//            NSString * mhour = [[hourName componentsSeparatedByString:@"点"] firstObject];
//            NSString * msecond = [[secondName componentsSeparatedByString:@"分"] firstObject];
//            NSString * cString = [[NSString alloc] initWithFormat:@"%@:%@",mhour,msecond];
//            
//            if ([cString compare:[dict valueForKey:@"DateS"]]>=0&&[cString compare:[dict valueForKey:@"DateE"]]<=0){
//                DDLOG(@"%@ %@ %@",dayName,hourName,secondName);
//                _sureBtn.enabled = NO;
//                isenable = NO;
//                break;
//            }
//            
//        }
//    }
//    if (isenable){
//        _sureBtn.enabled = YES;
//    }
    
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (timeType == 3){
        if (component == 0){
            return 140;
        }else if (component == 1){
            return 80;
        }else{
            return 80;
        }
    }
    if (component == 0){
        return 140;
    }else if (component == 1){
        return 90;
    }else{
        return 90;
    }
}

-(void)bgViewClick
{
    self.hidden = YES;
}

-(void)sureBtnClick
{
    self.hidden = YES;
    
    if (timeType == 3){
        [_delegate chooseTimeWithDay:[self.dayArray objectAtIndex:self.dayNumber] hour:[self.hourArray objectAtIndex:self.hourNumber] second:[self.secondArray objectAtIndex:self.secondNumber]];
    }else{
        [_delegate chooseTimeWithDay:[self.dayArray objectAtIndex:self.dayNumber] hour:[self.hourArray objectAtIndex:self.hourNumber] second:nil];
    }

}

-(void)setTheViewHidden
{
    self.hidden = YES;
}

-(void)setTheViewShow
{
    self.hidden = NO;
}

-(void)getDayDatas
{

    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps;
    comps = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[[NSDate alloc] init]];
    
    if (1){
        [_dayArray removeAllObjects];
        for (int i =0;i <=30;i++){
            [comps setDay:+i];
            [comps setHour:0];
            [comps setMinute:0];
            [comps setSecond:0];
            NSDate * nowDate = [calendar dateByAddingComponents:comps toDate:now options:0];
        
            NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
            [dateFormat setDateFormat:@"yyyy-MM-dd"];
            NSString *  locationString = [dateFormat stringFromDate:nowDate];
            [_dayArray addObject:locationString];
            
            NSDateFormatter* dateFormat0 = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
            [dateFormat0 setDateFormat:@"hh"];
            persentTime = [NSString stringWithFormat:@"%@",[dateFormat0 stringFromDate:now]];
            
        }
        
    }else{
        [_dayArray removeAllObjects];
        for (int i =3;i <=30;i++){
            [comps setDay:+i];
            [comps setHour:0];
            [comps setMinute:0];
            [comps setSecond:0];
            NSDate * nowDate = [calendar dateByAddingComponents:comps toDate:now options:0];

            NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
            [dateFormat setDateFormat:@"yyyy-MM-dd"];
            NSString *  locationString = [dateFormat stringFromDate:nowDate];
            [_dayArray addObject:locationString];
        }
    }
    [_pickerView reloadComponent:0];
}


-(void)setUserDateArray:(NSMutableArray *)userDateArray
{
    if (_userDateArray){
        [_userDateArray removeAllObjects];
    }
    _userDateArray= [[NSMutableArray alloc] initWithArray:userDateArray];
    NSString * dayName = [self.dayArray objectAtIndex:self.dayNumber];
    NSString * hourName = [self.hourArray objectAtIndex:self.hourNumber];
//    NSString * secondName = [self.secondArray objectAtIndex:self.secondNumber];
    
    BOOL isenable = YES;
    for (NSDictionary * dict in _yBuseDateArray){
        if ([dayName isEqualToString:dict[@"ShootDate"]]){
            _sureBtn.enabled = NO;
            isenable = NO;
            break;
        }
    }
    for (NSDictionary * dict in _userDateArray){
        if ([dayName isEqualToString:dict[@"ShootDate"]]&&[hourName isEqualToString:dict[@"ItemName"]]){
            _sureBtn.enabled = NO;
            isenable = NO;
            break;
        }
    }
    
    if (isenable){
        _sureBtn.enabled = YES;
    }
    
    _sureBtn.enabled = NO;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
